using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using taxi_sovereign.model;
using taxi_sovereign.controls;

namespace taxi_sovereign.view
{
        
    public partial class Form1 : Form
    {
        private int xlast = 0, ylast = 0;
        private bool move = false;
        private bool runrun = false;
        private bool foundcross = false;
        private interfaces.ICross movedc = null;
        private interfaces.ICross movedcc = null;
        private interfaces.IRoad movedr = null;
        private interfaces.IStop moveds = null;
        private interfaces.IVehicle movedv = null;
        private interfaces.IRoad[] route = new interfaces.IRoad[0];
        System.Drawing.Graphics graphicsObj;
        public Form1()
        {
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            graphicsObj = this.pictureBox1.CreateGraphics();
            Painter.SetGr(graphicsObj);
            Painter.LoadColorSchemaFromFile("Colors.tsc");
            ReconstrForm();
        }

        private void ReconstrForm()
        {
            groupBox1.Width = this.Width - groupBox2.Width - 40;
            pictureBox1.Width = this.Width - groupBox2.Width-50;
            groupBox1.Height = this.Height - groupBox3.Height - statusStrip1.Height - 120;
            pictureBox1.Height = this.Height - groupBox3.Height - statusStrip1.Height - 130;
            groupBox3.Width = groupBox1.Width;
            groupBox2.Height = groupBox1.Height;
            //Painter.PaintEverything();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
           //ReconstrForm();

            //graphicsObj = this.pictureBox1.CreateGraphics();

                   
            
            
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {

            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //pictureBox1.Refresh();
            //System.Drawing.Graphics graphicsObj;

            //graphicsObj = this.pictureBox1.CreateGraphics();
            //if (graphicsObj != null)
            {
                Painter.RefreshChosen();
                Painter.PaintEverything();
                richTextBox2.Text = MouseCtrl.Info();
                richTextBox1.Text = model.Logger.GetLast();
                analyze.Analyzer.AnlTick();
                analyze.Analyzer.AnlBusCapacity();
                analyze.Analyzer.AnlStopMood();
            }
            

            
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (!runrun)
            {
                toolStripButton4.Text = "Pause";
                timer1.Enabled = true;
                ModelCtrl.Start();
                runrun = true;
                
            }
            else
            {
                toolStripButton4.Text = "Run";
                timer1.Enabled = false;
                ModelCtrl.Stop();
                runrun = false;
            }



        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
           
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (Editor.edit)
            {
                
                if ((Editor.setroute))
                {
                    bool Found = false;
                    interfaces.ICross tempc = MouseCtrl.FindCross(e.X - Painter.XX(), e.Y - Painter.YY());
                    if ((tempc == null)&&(movedcc!=null))
                    {
                        movedcc = null;
                        interfaces.IStop[] stops = new Stop[0];
                        foreach (interfaces.IRoad r in route)
                        {
                            r.Chosen = true;
                            foreach (interfaces.IStop s in r.GetStopArray(r.GetFirstCross()))
                            {
                                Array.Resize(ref stops, stops.Length + 1);
                                stops[stops.Length - 1] = s;
                            }
                        }
                        if (movedv != null)
                        {
                            movedv.SetRoute(new Route(route, stops));
                            movedv.RebuildRoute();
                            movedv.Route().GetRoad()[0].DelTaxi(movedv);
                        }
                    Array.Resize(ref route, 0);
                    Array.Resize(ref stops, 0);
                    }
                    else
                    if (movedcc != null)
                    {
                        foreach (interfaces.IRoad r in movedcc.GetRoads())
                        {
                            if ((((r.GetFirstCross().Equals(tempc)&&(r.GetFirstCross() != movedcc)) || ((r.GetSecondCross().Equals(tempc))&&(r.GetSecondCross() != movedcc)))))
                                
                            {
                                r.Chosen = true;
                                Array.Resize(ref route, route.Length + 1);
                                route[route.Length - 1] = r;
                                Found = true;
                                break;
                            }
                        }
                        movedcc = tempc;

                    
                    if ((!Found)&&(movedv!=null))
                    {
                        movedcc = null;
                        movedv.SetRoute(new Route(route, new Stop[0]));
                    }
                    }
                    else 
                    {
                        movedcc = tempc;
                    }


                }
                else
                if (Editor.newvehicle)
                {
                    interfaces.IRoad tempr = MouseCtrl.FindRoad(e.X - Painter.XX(), e.Y - Painter.YY());
                    if (tempr != null)
                    {
                        model.Route temproute = new Route(new Road[] { (Road)tempr }, tempr.GetStopArray(tempr.GetFirstCross()));
                        /*interfaces.IVehicle veh = new Vehicle((textBox1.Text.ToString()),
                                                new Route(new Road[] { (Road)tempr }, tempr.GetStopArray(tempr.GetFirstCross()))
                                                //temproute, 
                                                , tempr, tempr.GetFirstCross());
                        */
                        interfaces.IVehicle veh = new Vehicle((textBox1.Text.ToString()), temproute, temproute.GetRoad()[0], temproute.GetRoad()[0].GetFirstCross());
                        ((Model)MouseCtrl.currmodel()).GetModelSettings().Vehicles().Add(veh);
                        //########################################
                        tempr.AddTaxi(veh);
                    }
                }
                else
                if (Editor.newstop)
                {
                    interfaces.IRoad tempr = MouseCtrl.FindRoad(e.X - Painter.XX(), e.Y - Painter.YY());
                    if (tempr != null)
                    {
                        tempr.AddStop(new Stop((float)Math.Sqrt((double)((e.X - Painter.XX() - tempr.GetFirstCross().GetPositionX()) *
                                                    (e.X - Painter.XX() - tempr.GetFirstCross().GetPositionX())
                                                    +
                                                    (e.Y - Painter.YY() - tempr.GetFirstCross().GetPositionY()) *
                                                    (e.Y - Painter.YY() - tempr.GetFirstCross().GetPositionY())))));
                        MouseCtrl.RebuildRoutes();
                    }
                }
                else
                if (Editor.newroad)
                {
                    Cross temp = (Cross)MouseCtrl.FindCross(e.X - Painter.XX(), e.Y - Painter.YY());
                    if (temp is Cross)
                    {
                        Editor.SetFirstObject(temp);
                        Editor.objectready = true;
                    }
                    else
                        if (Editor.objectready)
                        {
                            Editor.NewRoadToPoint(e.X - Painter.XX(), e.Y - Painter.YY(), textBox1.Text.ToString());
                            //Editor.objectready = false;
                            Editor.SetFirstObject(MouseCtrl.FindCross(e.X - Painter.XX(), e.Y - Painter.YY()));
                        }
                }
                else
                if (Editor.newcross)
                {
                    Cross temp = new Cross(e.X - Painter.XX(), e.Y - Painter.YY());
                    Editor.NewCross(temp);
                    Editor.SetFirstObject(temp);
                    Editor.objectready = true;
                }
                else
                    if (Editor.mergecrosses)
                    {
                        Cross temp = (Cross)MouseCtrl.FindCross(e.X - Painter.XX(), e.Y - Painter.YY());
                        if ((temp is Cross) && ((!Editor.objectready) || (!(Editor.chosenObject is interfaces.ICross))))
                        {
                            Editor.SetFirstObject(temp);
                            Editor.objectready = true;
                        }
                        else
                            if ((temp is Cross) && (Editor.objectready) && (Editor.chosenObject is interfaces.ICross))
                            {
                                //foreach (interfaces.IRoad r in ((interfaces.ICross)Editor.chosenObject).GetRoads())
                                {
                                    interfaces.IRoad temproad = new Road(temp, (interfaces.ICross)Editor.chosenObject,textBox1.Text.ToString());
                                    //temp.AddRoad(temproad);
                                    //temp.AddRoad(temproad);
                                    ((model.Model)(Editor.currmodel)).GetModelSettings().Roads().Add(temproad);
                                }
                                Editor.chosenObject = temp;
                            }

                    }
                    else
                    {
                        movedv = MouseCtrl.FindVehicle(e.X, e.Y);
                    }
            }
            richTextBox2.Text = taxi_sovereign.controls.MouseCtrl.FindObject(e.X-Painter.XX(), e.Y-Painter.YY());
        
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Logger.SaveLog("Log.txt");
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Preferences prefs = new Preferences();
            prefs.Show();
            prefs.Activate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Painter.PaintEverything();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Logger.SaveLog("Log.txt");
        }

        private void openModelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ModelCtrl.LoadModelSettings(openFileDialog1.FileName);
                Form1.ActiveForm.Text = openFileDialog1.FileName+" - taxi-sovereign";
                Painter.PaintEverything();
                Painter.PaintEverything();
                //Form1.ActiveForm.Refresh();
            }
        }

        private void saveModelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ModelCtrl.SaveModelSettings(saveFileDialog1.FileName);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About abouT = new About();
            abouT.Show();
            abouT.Activate();

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (Editor.moving)
            {
                moveds = MouseCtrl.FindStop(e.X - Painter.XX(), e.Y - Painter.YY());
                if (moveds == null)
                {
                    movedc = MouseCtrl.FindCross(e.X - Painter.XX(), e.Y - Painter.YY());
                    if (movedc == null)
                    {
                        movedr = MouseCtrl.FindRoad(e.X - Painter.XX(), e.Y - Painter.YY());
                    }
                }
                if (moveds != null)
                {
                    movedr = MouseCtrl.FindRoad(e.X - Painter.XX(), e.Y - Painter.YY());
                }
                xlast = e.X;
                ylast = e.Y;
            }

            if (Editor.scrolling) 
            {
                if ((movedc == null)&&(movedr == null)&&(moveds == null))
                {
                    move = true;
                    xlast = e.X;
                    ylast = e.Y;
                }
                
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (Editor.moving)
            {
                if ((moveds != null)&&(movedr != null))
                {
                    interfaces.IRoad tempr = MouseCtrl.FindRoad(e.X - Painter.XX(), e.Y - Painter.YY());
                    if (tempr != null)
                    {
                        tempr.AddStop(moveds);
                        movedr.DelStop(moveds);
                        moveds.Distance = (float)Math.Sqrt((double)((e.X - Painter.XX() - tempr.GetFirstCross().GetPositionX()) *
                                                    (e.X - Painter.XX() - tempr.GetFirstCross().GetPositionX())
                                                    +
                                                    (e.Y - Painter.YY() - tempr.GetFirstCross().GetPositionY()) *
                                                    (e.Y - Painter.YY() - tempr.GetFirstCross().GetPositionY())));
                        MouseCtrl.RebuildRoutes();
                    }
                    movedr = null;
                }
                else
                if (movedc != null)
                {
                    movedc.SetPositionX(e.X - Painter.XX());
                    movedc.SetPositionY(e.Y - Painter.YY());
                    foreach (interfaces.IRoad r in movedc.GetRoads())
                    {
                        r.Reconstr();
                    }
                    movedc = null;
                    Painter.PaintEverything();
                }
                else
                if (movedr != null)
                {
                    movedr.GetFirstCross().SetPositionX(movedr.GetFirstCross().GetPositionX() + e.X -  xlast);
                    movedr.GetFirstCross().SetPositionY(movedr.GetFirstCross().GetPositionY() + e.Y -  ylast);
                    movedr.GetSecondCross().SetPositionX(movedr.GetSecondCross().GetPositionX() + e.X -  xlast);
                    movedr.GetSecondCross().SetPositionY(movedr.GetSecondCross().GetPositionY() + e.Y -  ylast);

                    foreach (interfaces.IRoad r in movedr.GetFirstCross().GetRoads())
                    {
                        r.Reconstr();
                    }
                    foreach (interfaces.IRoad r in movedr.GetSecondCross().GetRoads())
                    {
                        r.Reconstr();
                    }
                    movedr = null;
                }
            }
            if (move)
            {
                move = false;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (Editor.moving)
            {
                if (movedc != null)
                {
                    movedc.SetPositionX(e.X - Painter.XX());
                    movedc.SetPositionY(e.Y - Painter.YY());
                    foreach (interfaces.IRoad r in movedc.GetRoads())
                    {
                        r.Reconstr();
                    }
                    
                    Painter.PaintEverything();
                }
                else
                    if ((movedr != null)&&(moveds ==null))
                    {
                        movedr.GetFirstCross().SetPositionX(movedr.GetFirstCross().GetPositionX() + e.X - xlast);
                        movedr.GetFirstCross().SetPositionY(movedr.GetFirstCross().GetPositionY() + e.Y -ylast);
                        movedr.GetSecondCross().SetPositionX(movedr.GetSecondCross().GetPositionX() + e.X - xlast);
                        movedr.GetSecondCross().SetPositionY(movedr.GetSecondCross().GetPositionY() + e.Y - ylast);
                        foreach (interfaces.IRoad r in movedr.GetFirstCross().GetRoads())
                        {
                            r.Reconstr();
                        } 
                        foreach (interfaces.IRoad r in movedr.GetSecondCross().GetRoads())
                        {
                            r.Reconstr();
                        }
                        xlast = e.X;
                        ylast = e.Y;
                    }
            }
                
            if (Editor.scrolling)
            if (move)
            {
                if (movedc == null)
                Painter.PlusSetXY(e.X - xlast, e.Y - ylast);
                xlast = e.X;
                ylast = e.Y;
                Painter.PaintEverything();
            }
        }

        private void richTextBox1_Click(object sender, EventArgs e)
        {
            LogForm lgf = new LogForm();
            lgf.Activate();
            lgf.Show();
        }

        private void analyzerToolStripMenuItem_Click(object sender, EventArgs e)
        {

            AnalyzerForm lgf = new AnalyzerForm();
            lgf.Activate();
            lgf.Show();
        }

 private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            ReconstrForm();
        }

        private void Form1_MaximizedBoundsChanged(object sender, EventArgs e)
        {
            ReconstrForm();
        }

        private void Form1_ResizeBegin(object sender, EventArgs e)
        {
            ReconstrForm();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void PaintTimer_Tick(object sender, EventArgs e)
        {
            Painter.PaintEverything();
        }

        private void ScrollingCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (ScrollingCheck.Checked) Editor.scrolling = true;
            else Editor.scrolling = false;

            
        }

        private void EditCheck_CheckedChanged(object sender, EventArgs e)
        {
            Editor.SetFalse();
            if (EditCheck.Checked) Editor.edit = true;
            else Editor.edit = false;
            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Editor.SetFalse();
            if (MoveCheck.Checked) Editor.moving = true;
            else Editor.moving = false; 
            
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            Editor.SetFalse();
            if (radioButton1.Checked) Editor.newroad = true;
            else Editor.newroad = false; 
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            Editor.SetFalse();
            if (radioButton2.Checked) Editor.newcross = true;
            else Editor.newcross = false; 
        }

        private void RadioButton3_CheckedChanged(object sender, EventArgs e)
        {
            Editor.SetFalse();
            if (radioButton3.Checked) Editor.newstop = true;
            else Editor.newstop = false; 
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            Editor.SetFalse();
            if (radioButton4.Checked) Editor.newvehicle = true;
            else Editor.newvehicle = false; 
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            Editor.SetFalse();
            if (radioButton5.Checked) Editor.setroute = true;
            else Editor.setroute = false; 
        }

        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                radioButton1.Enabled = true;
                radioButton2.Enabled = true;
                radioButton3.Enabled = true;
                radioButton4.Enabled = true;
                radioButton5.Enabled = true;
                radioButton5.Enabled = true;
                radioButton6.Enabled = true;
                textBox1.Enabled = true;
                
            }
            else
            {
                radioButton1.Enabled = false;
                radioButton2.Enabled = false;
                radioButton3.Enabled = false;
                radioButton4.Enabled = false;
                radioButton5.Enabled = false;
                radioButton6.Enabled = false;
                radioButton1.Checked = false;
                radioButton2.Checked = false;
                radioButton3.Checked = false;
                radioButton4.Checked = false;
                radioButton5.Checked = false;
                radioButton6.Checked = false;
                textBox1.Enabled = false;
            }
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
 Editor.SetFalse();

            if (radioButton6.Checked) Editor.mergecrosses = true;
            else Editor.mergecrosses = false; 
        }

        private void rESETToolStripMenuItem_Click(object sender, EventArgs e)
        {
            (Editor.currmodel).Reset();
        }

        
    }
}